package com.wuxu.huanfu

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater.Factory2
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate

/**
 *@author wuxu
 *@date 2024/5/20 09:46
 *@description
 */
class WXFactory(private val activity: Activity,val resMgr: ResourceMgr) : Factory2 {
    companion object{
        val TAG = "WXFactory"
    }

    private var mDelegate: AppCompatDelegate? = null
    override fun onCreateView(
        parent: View?,
        name: String,
        context: Context,
        attrs: AttributeSet
    ): View? {
        if (mDelegate == null) {
            mDelegate = if (activity is AppCompatActivity) {
                activity.delegate
            } else {
                AppCompatDelegate.create(activity, null)
            }
        }

        val view = mDelegate?.createView(parent, name, context, attrs)

        view?.let {
            if (it is TextView) {
                it.text = "wuxu"

                parseAttr(it, attrs)
            }
        }

        return view
    }


    override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
        return null
    }

    //为了学习怎么获取、解析xml中的数据，这里做一个练习方法
    private fun parseAttr(view: TextView, attrs: AttributeSet) {

        for (i in 0 until attrs.attributeCount) {

            val attrName: String = attrs.getAttributeName(i)// xml中属性名称
            val attrValue: String = attrs.getAttributeValue(i)// xml中属性类型

            Log.i(
                TAG,
                "    attributeName:$attrName | attrValue:$attrValue"
            )


            if ("style" == attrName) {//如果是style 属性，获取具体信息
                val skinAttrs = IntArray(2)
                skinAttrs[0] = android.R.attr.background

                val typedArray = view.context.obtainStyledAttributes(attrs, skinAttrs, 0, 0)

                val textColorId = typedArray.getResourceId(0, -1)

                if (textColorId != -1) {
                    val entryName = view.context.resources.getResourceEntryName(textColorId)
                    val typeName = view.context.resources.getResourceTypeName(textColorId)

                    Log.i(
                        TAG,
                        """   textColor in style is supported:
                                resource id:$textColorId
                                attrName:$attrName
                                attrValue:$attrValue
                                entryName:$entryName
                                typeName:$typeName"""
                    )


                    Log.i(WXFactory.TAG,"resource -> ${resMgr.mSkinPackageName}")
                    resMgr.mResource?.let {
                        Log.i(WXFactory.TAG,"www -> ${it.getIdentifier("skin_green","color",resMgr.mSkinPackageName)}")
                        view.setTextColor(it.getColor(it.getIdentifier("skin_green","color",resMgr.mSkinPackageName)))
                    }

                }

                typedArray.recycle()

                continue

            }



            if (attrValue.startsWith('@')){
                val id = attrValue.substring(1).toInt()

                val entryName = view.context.resources.getResourceEntryName(id)
                val typeName =  view.context.resources.getResourceTypeName(id)

                Log.i(
                   TAG,
                    """    $attrName is supported:
                    resource id:$id
                    attrName:$attrName
                    attrValue:$attrValue
                    entryName:$entryName
                    typeName:$typeName"""
                )
            }
        }
    }

    init {

    }
}